{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib\n",
    "# this is our global size of label text  on the plots\n",
    "matplotlib.rc('xtick', labelsize=20) \n",
    "matplotlib.rc('ytick', labelsize=20) \n",
    "\n",
    "# This line ensures that the plot is displayed\n",
    "# inside the notebook\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((8693, 14), (4277, 13))"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train = pd.read_csv('../input/train.csv')\n",
    "test = pd.read_csv('../input/test.csv')\n",
    "\n",
    "train.shape, test.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0    8291\n",
       " 1     199\n",
       " Name: VIP, dtype: int64,\n",
       " 0    4110\n",
       " 1      74\n",
       " Name: VIP, dtype: int64)"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.loc[train['VIP'] == False, 'VIP'] = 0\n",
    "train.loc[train['VIP'] == True, 'VIP'] = 1\n",
    "test.loc[test['VIP'] == False, 'VIP'] = 0\n",
    "test.loc[test['VIP'] == True, 'VIP'] = 1\n",
    "\n",
    "train.VIP.value_counts(),  test.VIP.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0    8494\n",
       " 1     199\n",
       " Name: VIP, dtype: int64,\n",
       " 0    4203\n",
       " 1      74\n",
       " Name: VIP, dtype: int64)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.VIP.fillna(0, inplace=True)\n",
    "test.VIP.fillna(0, inplace=True)\n",
    "train.VIP.value_counts(),  test.VIP.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False    0.641694\n",
      "True     0.358306\n",
      "Name: CryoSleep, dtype: float64\n",
      "False    0.630975\n",
      "True     0.369025\n",
      "Name: CryoSleep, dtype: float64\n",
      "============================\n",
      "0    0.650638\n",
      "1    0.349362\n",
      "Name: CryoSleep, dtype: float64\n",
      "0    0.638999\n",
      "1    0.361001\n",
      "Name: CryoSleep, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "print(train.CryoSleep.value_counts(normalize=True))\n",
    "print(test.CryoSleep.value_counts(normalize=True))\n",
    "train.loc[train['CryoSleep'] == False, 'CryoSleep'] = 0\n",
    "train.loc[train['CryoSleep'] == True, 'CryoSleep'] = 1\n",
    "test.loc[test['CryoSleep'] == False, 'CryoSleep'] = 0\n",
    "test.loc[test['CryoSleep'] == True, 'CryoSleep'] = 1\n",
    "train.CryoSleep.fillna(0, inplace=True)\n",
    "test.CryoSleep.fillna(0, inplace=True)\n",
    "print(\"============================\")\n",
    "print(train.CryoSleep.value_counts(normalize=True))\n",
    "print(test.CryoSleep.value_counts(normalize=True))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "TRAPPIST-1e      0.680433\n",
      "55 Cancri e      0.207063\n",
      "PSO J318.5-22    0.091568\n",
      "NONE             0.020936\n",
      "Name: Destination, dtype: float64\n",
      "TRAPPIST-1e      0.691139\n",
      "55 Cancri e      0.196633\n",
      "PSO J318.5-22    0.090718\n",
      "NONE             0.021510\n",
      "Name: Destination, dtype: float64\n",
      "===============================\n",
      "TRAPPIST-1e      5915\n",
      "55 Cancri e      1800\n",
      "PSO J318.5-22     978\n",
      "Name: Destination, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "train.Destination.fillna('NONE', inplace=True)\n",
    "test.Destination.fillna('NONE', inplace=True)\n",
    "print(train.Destination.value_counts(normalize=True))\n",
    "print(test.Destination.value_counts(normalize=True))\n",
    "print(\"===============================\")\n",
    "train.loc[train['Destination'] == 'NONE', 'Destination'] = 'PSO J318.5-22'\n",
    "test.loc[test['Destination'] == 'NONE', 'Destination'] = 'PSO J318.5-22'\n",
    "print(train.Destination.value_counts())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8693, 17) (4277, 16)\n",
      "(8693, 15) (4277, 14)\n"
     ]
    }
   ],
   "source": [
    "train[['Deck','Num','Side']] = train['Cabin'].str.split('/', expand=True)\n",
    "test[['Deck','Num','Side']] = test['Cabin'].str.split('/', expand=True)\n",
    "print(train.shape, test.shape)\n",
    "train = train.drop(columns=['Cabin', 'Name'])\n",
    "test = test.drop(columns=['Cabin', 'Name'])\n",
    "print(train.shape, test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in train.columns:\n",
    "    if train[i].dtypes in ['object','bool']:\n",
    "        train[i].fillna(train[i].mode()[0],inplace=True)\n",
    "    else :\n",
    "        train[i].fillna(train[i].mean(),inplace=True)\n",
    "\n",
    "train['Num'] = train['Num'].astype(int)\n",
    "\n",
    "for i in test.columns:\n",
    "    if test[i].dtypes in ['object','bool']:\n",
    "        test[i].fillna(test[i].mode()[0],inplace=True)\n",
    "    else :\n",
    "        test[i].fillna(test[i].mean(),inplace=True)\n",
    "\n",
    "test['Num'] = test['Num'].astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "train.to_csv('../input/train_clear.csv', index=False)\n",
    "test.to_csv('../input/test_clear.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "####### 已经做完的\n",
    "# 将布尔值进行转换，已经清理好了文件并保存下在 xx_clear.csv\n",
    "\n",
    "####### 准备做的\n",
    "# 不要name，使用one-hot进行编码，然后用LR计算模型"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
